package components;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;

import javax.swing.*;
import javax.swing.filechooser.*;
 
public class FileChooser extends JPanel{

	private static final long serialVersionUID = 1L;
	static private final String newline = "\n";
    File inputFile;
    File outputFile;
	JButton openButton, saveButton;
    JTextArea log;
    JFileChooser fc;
 
    public FileChooser() {
        super(new BorderLayout());
 
        log = new JTextArea(5,20);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);
 
        fc.setCurrentDirectory(new File(""));
 
        openButton = new JButton("Apri");
        openButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
	            int returnVal = fc.showOpenDialog(FileChooser.this);
	            
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	            	inputFile = fc.getSelectedFile();
	                //This is where a real application would open the file.
	                log.setText("");
	                log.append("Aperto: " + inputFile.getName() + "." + newline);
	            } 
	            log.setCaretPosition(log.getDocument().getLength());
				
			}});
        //Create the save button.  We use the image from the JLF
        //Graphics Repository (but we extracted it from the jar).
        saveButton = new JButton("Converti");
        saveButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
	            int returnVal = fc.showSaveDialog(FileChooser.this);
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	                outputFile = fc.getSelectedFile();
	                //This is where a real application would save the file.
	                log.setText("");
	                log.append("Converto in: " + outputFile.getName() + "." + newline);
	            }
			
				try {
					BufferedReader input=new BufferedReader(new FileReader(inputFile));
					int X=0;
					int Y=0;
					String str;
					str=input.readLine();
					String[] row;
					PrintWriter writer = new PrintWriter(outputFile);
					int lines = 1;
					while (input.readLine() != null) lines++;
					input.close();
					input=new BufferedReader(new FileReader(inputFile));
					while (str!=null){
						row=str.split("\t");
						for (String string : row) {
							int val=Integer.parseInt(string);
							writer.println(X+"\t"+(lines-Y)+"\t"+val);
							X++;
						}
						str=input.readLine();
						Y++;
						X=0;
					}

					if ( input != null)
						input.close( );
					
					if ( writer != null)
						writer.close( );
					log.append("Convertito!!");
				} catch (Exception e1) {
					log.append("ERRORE!");
					log.append(e1.getMessage());
				}

			
			}});     
			
	
 
        //For layout purposes, put the buttons in a separate panel
        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(openButton);
        buttonPanel.add(saveButton);
 
        //Add the buttons and the log to this panel.
        add(buttonPanel, BorderLayout.PAGE_START);
        add(logScrollPane, BorderLayout.CENTER);
    }
 

 
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("FileChooser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Add content to the window.
        frame.add(new FileChooser());
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
 
}
